// pages/cards/cards.js
Page({

  /**
   * Page initial data
   */
  data: {
    cards: [
      {cardNumber: '123443534'},
      {cardNumber: '222443534'},
      {cardNumber: '333443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},
      {cardNumber: '222443534'},

    ],
    userAccount: wx.getStorageSync('localUserAccount')

  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {
    this.get_all_cards()

    console.log('加载方法...')

  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage() {

  },
  add_bind_info:function (e){
    var cardNumber =e.currentTarget.dataset.word
    console.log('卡号是：'+cardNumber)
    console.log('账号是：'+this.data.userAccount)
    var app =getApp()
    // 请求绑定
    wx.request({
      method:'post',
      url: app.globalData.baseUrl + '/user/bindCard',
      data:{
        userAccount: this.data.userAccount,
        cardNumber:cardNumber
      },
      success(res){
        if (res.data==0){
          wx.showToast({
            title: '绑定成功！',
          })
          wx.navigateTo({
            url: '../student/student',
          })
        }
        else{
          wx.showToast({
            title: '绑定失败！',
          })
        }
      }
    })


  },
  get_all_cards:function(){
    var app =getApp()
    var that =this
    wx.request({
      url: app.globalData.baseUrl +'/card/getAllCards', // 仅为示例，并非真实的接口地址
      method: 'post',
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        if (res.data !=null) {
          wx.showToast({
            title: '获取卡号成功！',
            icon: 'none',
            duration: 1500
          })
          that.data.cards = res.data
          that.setData({
            cards: that.data.cards
          })
          console.log('更新视图成功')


        } else {
          wx.showToast({
            title: '获取卡失败',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })

  }
})